import Vue from "vue";
import VueRouter from "vue-router";
import Index from "../views/Index.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/search/:kw?",
    name: "search",
    props: true,
    component: () => import("../views/Search.vue"),
    meta: {
      title: "公司列表-卷心菜",
    },
  },
  {
    path: "/details/:jid",
    props: true,
    name: "details",
    component: () => import("../views/Details.vue"),
    meta: {
      title: "详情页-卷心菜",
    },
  },
  {
    path: "/resume/:jid",
    name: "resume",
    props: true,
    component: () => import("../views/Resume.vue"),
    meta: {
      title: "个人中心-卷心菜",
      requireAuth: true,
    },
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/Login.vue"),
    meta: {
      title: "登录页面-卷心菜",
      hideHeader: true,
      hideFooter: true,
    },
  },
  {
    path: "/register",
    name: "register",
    component: () => import("../views/Register.vue"),
    meta: {
      title: "注册页面-卷心菜",
      hideHeader: true,
      hideFooter: true,
    },
  },
  {
    path: "/",
    name: "index",
    component: Index,
    meta: {
      title: "首页-卷心菜",
    },
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    if (sessionStorage.getItem("uname")) {
      next();
    } else {
      alert("请先登录");
      next({ path: "/login" });
    }
  }
  document.title = to.meta.title;
  next();
});

export default router;
